package com.volunteer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.volunteer.common.BusinessException;
import com.volunteer.common.ResultCode;
import com.volunteer.entity.Carousel;
import com.volunteer.mapper.CarouselMapper;
import com.volunteer.model.vo.HomeDataVO;
import com.volunteer.service.CarouselService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 轮播图服务实现类
 */
@Service
@RequiredArgsConstructor
public class CarouselServiceImpl extends ServiceImpl<CarouselMapper, Carousel> implements CarouselService {

    /**
     * 创建轮播图
     *
     * @param carousel 轮播图信息
     * @return 是否成功
     */
    @Override
    public boolean createCarousel(Carousel carousel) {
        if (carousel == null || carousel.getImageUrl() == null) {
            throw new BusinessException(ResultCode.PARAM_ERROR);
        }

        // 设置默认值
        carousel.setStatus(1); // 默认启用
        carousel.setCreateTime(LocalDateTime.now());
        carousel.setUpdateTime(LocalDateTime.now());

        if (carousel.getSort() == null) {
            carousel.setSort(0);
        }

        return this.save(carousel);
    }

    /**
     * 更新轮播图
     *
     * @param carousel 轮播图信息
     * @return 是否成功
     */
    @Override
    public boolean updateCarousel(Carousel carousel) {
        if (carousel == null || carousel.getId() == null) {
            throw new BusinessException(ResultCode.PARAM_ERROR);
        }

        // 检查轮播图是否存在
        Carousel existCarousel = this.getById(carousel.getId());
        if (existCarousel == null) {
            throw new BusinessException(ResultCode.NOT_FOUND);
        }

        // 更新时间
        carousel.setUpdateTime(LocalDateTime.now());

        return this.updateById(carousel);
    }

    /**
     * 删除轮播图
     *
     * @param id 轮播图ID
     * @return 是否成功
     */
    @Override
    public boolean deleteCarousel(Long id) {
        if (id == null) {
            throw new BusinessException(ResultCode.PARAM_ERROR);
        }

        // 检查轮播图是否存在
        Carousel carousel = this.getById(id);
        if (carousel == null) {
            throw new BusinessException(ResultCode.NOT_FOUND);
        }

        return this.removeById(id);
    }

    /**
     * 获取所有启用的轮播图
     *
     * @return 轮播图列表
     */
    @Override
    public List<Carousel> listEnabledCarousels() {
        LambdaQueryWrapper<Carousel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Carousel::getStatus, 1) // 启用状态
                .orderByAsc(Carousel::getSort)
                .orderByDesc(Carousel::getCreateTime);

        return this.list(queryWrapper);
    }

    /**
     * 分页查询轮播图
     *
     * @param page     分页参数
     * @param carousel 查询条件
     * @return 轮播图列表
     */
    @Override
    public Page<Carousel> pageCarousels(Page<Carousel> page, Carousel carousel) {
        LambdaQueryWrapper<Carousel> queryWrapper = new LambdaQueryWrapper<>();

        if (carousel != null) {
            // 按状态查询
            if (carousel.getStatus() != null) {
                queryWrapper.eq(Carousel::getStatus, carousel.getStatus());
            }

            // 按活动ID查询
            if (carousel.getActivityId() != null) {
                queryWrapper.eq(Carousel::getActivityId, carousel.getActivityId());
            }

            // 按标题查询
            if (carousel.getTitle() != null) {
                queryWrapper.like(Carousel::getTitle, carousel.getTitle());
            }
        }

        // 按排序和创建时间排序
        queryWrapper.orderByAsc(Carousel::getSort)
                .orderByDesc(Carousel::getCreateTime);

        return this.page(page, queryWrapper);
    }

    /**
     * 启用/禁用轮播图
     *
     * @param id     轮播图ID
     * @param status 状态（0-禁用 1-启用）
     * @return 是否成功
     */
    @Override
    public boolean setCarouselStatus(Long id, Integer status) {
        if (id == null || status == null) {
            throw new BusinessException(ResultCode.PARAM_ERROR);
        }

        // 检查轮播图是否存在
        Carousel carousel = this.getById(id);
        if (carousel == null) {
            throw new BusinessException(ResultCode.NOT_FOUND);
        }

        // 更新状态
        carousel.setStatus(status);
        carousel.setUpdateTime(LocalDateTime.now());

        return this.updateById(carousel);
    }

    @Override
    public List<HomeDataVO.CarouselVO> getCarouselList() {
        return super.list().stream().map(carousel -> {
            HomeDataVO.CarouselVO carouselVO = new HomeDataVO.CarouselVO();
            carouselVO.setId(carousel.getId());
            carouselVO.setTitle(carousel.getTitle());
            carouselVO.setImageUrl(carousel.getImageUrl());
            carouselVO.setLinkUrl(carousel.getLinkUrl());
            carouselVO.setSort(carousel.getSort());
            return carouselVO;
        }).collect(Collectors.toList());
    }
}
